package com.atguigu.jxc.controller;

import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReturnListGoods")
public class CustomerReturnListGoodsController {

    @Autowired
    private UserDao userDao;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerReturnListDao customerReturnListDao;


    /**客户退货单保存
     * http://localhost:8080/customerReturnListGoods/save?returnNumber=XT1605772786495（退货单号）
     */
    @PostMapping("/save")
    @ResponseBody
    public ServiceVO saveReturnList(CustomerReturnList customerReturnList, String customerReturnListGoodsStr){

        if (customerReturnList == null || StringUtils.isEmpty(customerReturnListGoodsStr)){
            return null;
        }
        //将字符串转换为集合
        Gson gson = new Gson();
        List<CustomerReturnListGoods> customerReturnListGoodsList = gson.fromJson(customerReturnListGoodsStr, new TypeToken<List<CustomerReturnListGoods>>() {}.getType());
        //设置当前操作用户
        User user = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
        //将用户Id放入
        customerReturnList.setTrueName(user.getTrueName());

        Customer customer = customerService.findById(customerReturnList.getCustomerId());
        customerReturnList.setCustomerName(customer.getCustomerName());

        customerReturnListDao.insertCustomerReturnListDao(customerReturnList);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**客户退货单查询（可查询条件：退货单号、客户、退款状态）
     * http://localhost:8080/customerReturnListGoods/list
     */
    @PostMapping()
    @ResponseBody
    public Map<String,Object> listCustomerReturn(String returnNumber, Integer customerId, Integer state, String sTime,String eTime){
        List<CustomerReturnList> listCRL = customerReturnListDao.findCustomerReturnList(returnNumber,customerId,state,sTime,eTime);
        if(listCRL == null){
            return null;
        }
        Map<String,Object> map = new HashMap<>();
        for (CustomerReturnList customerReturnList : listCRL) {
            Customer customer = customerService.findById(customerReturnList.getCustomerId());
            customerReturnList.setCustomerName(customer.getCustomerName());
            User user = userDao.getUserById(customerReturnList.getUserId());
            customerReturnList.setTrueName(user.getTrueName());
            map.put("rows", customerReturnList);
        }
        return map;
    }

    /**退货单商品信息
     *http://localhost:8080/customerReturnListGoods/goodsList
     */
    @PostMapping("/goodsList")
    @ResponseBody
    public Map<String,Object> findGoodsList(Integer customerReturnListId){
        List<CustomerReturnListGoods> list = customerReturnListDao.findCRLG(customerReturnListId);
        if(list == null){
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        return map;
    }

    /**删除退货单
     * http://localhost:8080/customerReturnListGoods/delete
     */
    @PostMapping("/delete")
    @ResponseBody
    public ServiceVO deleteCRL(Integer customerReturnListId){
        customerReturnListDao.deleteCPLById(customerReturnListId);

        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 客户退货统计（可根据 商品类别、商品编码或名称 条件查询）
     *http://localhost:8080/customerReturnListGoods/count
     */
    @PostMapping("/count")
    @ResponseBody
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName){
        List list = customerReturnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        if (list == null){
            return null;
        }
        return list.toString();
    }
}
